package com.tyun.modules.sys.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.tyun.common.utils.R;
import com.tyun.modules.sys.entity.SysDept;
import com.tyun.modules.sys.service.ISysDeptService;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 部门
 *
 * @author congcong tao
 * @date 2018/6/11 16:03
 */
@RestController
@RequestMapping("sysDept")
public class SysDeptController extends CommonController{

    private static final Logger logger = LogManager.getLogger(SysDeptController.class);

    @Autowired
    private ISysDeptService sysDeptService;

    @GetMapping("/list")
    public R list(SysDept sysDept){
        sysDept.setRemovetag("N");
        EntityWrapper<SysDept> ew = new EntityWrapper<>(sysDept);
        List<SysDept> sysDeptList = sysDeptService.selectList(ew);
        return R.ok().put("sysDeptList", sysDeptList);
    }

    @GetMapping("/listChildren")
    public JSONArray listChildren(SysDept sysDept){
        sysDept.setRemovetag("N");
        EntityWrapper<SysDept> ew = new EntityWrapper<>(sysDept);
        List<SysDept> sysDeptList = sysDeptService.selectList(ew);
        JSONArray rows = new JSONArray();
        if (sysDeptList != null && sysDeptList.size() > 0) {
            JSONObject row = null;
            for (SysDept item : sysDeptList) {
                row = new JSONObject();
                row.put("deptName", item.getDeptName());
                row.put("orderNum", item.getOrderNum());
                row.put("Id", item.getId());
                row.put("status", item.getStatus());
                row.put("createTime", item.getCreateTime());
                if (!StringUtils.equals(item.getParentId(),"0")) {
                    row.put("ParentId", item.getParentId());
                }
                rows.add(row);
            }
        }
        return rows;
    }
}
